package com.towin.org.esdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.towin.org.esdemo.controller.vo.SystemHelpCenterCreateVO;
import com.towin.org.esdemo.dal.dataobject.SystemHelpCenterDO;
import com.towin.org.esdemo.es.entity.SystemHelpCenterDTO;
import com.towin.org.esdemo.mapper.SystemHelpCenterMapper;
import com.towin.org.esdemo.repository.SystemHelpCenterRepository;
import com.towin.org.esdemo.service.SystemHelpCenterService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wzx
 * @date 2025/6/6 9:44
 * @description: SystemHelpCenterServiceImpl
 */
@Service
public class SystemHelpCenterServiceImpl extends ServiceImpl<SystemHelpCenterMapper, SystemHelpCenterDO> implements SystemHelpCenterService {

    @Resource
    private SystemHelpCenterRepository systemHelpCenterRepository;


    @Override
    public void create(SystemHelpCenterCreateVO systemHelpCenterCreateVO) {
        SystemHelpCenterDO systemHelpCenterDO = new SystemHelpCenterDO();
        BeanUtils.copyProperties(systemHelpCenterCreateVO, systemHelpCenterDO);
        systemHelpCenterDO.setCreateTime(LocalDateTime.now());
        this.save(systemHelpCenterDO);

        // 存入es  只存储有内容的文章
        if (systemHelpCenterCreateVO.getType() == 1){
            SystemHelpCenterDTO systemHelpCenterDTO = new SystemHelpCenterDTO();
            BeanUtils.copyProperties(systemHelpCenterCreateVO, systemHelpCenterDTO);
            systemHelpCenterDTO.setId(systemHelpCenterDO.getId());
            systemHelpCenterDTO.setCreateTime(LocalDateTime.now());
            // 根据父级id 拿到上级名称
            SystemHelpCenterDO parentDO = this.getById(systemHelpCenterCreateVO.getParentId());
            systemHelpCenterDTO.setParentName(parentDO.getTitle());
            // 查询到顶级id 和名称
            SystemHelpCenterDO topDo = this.getById(parentDO.getParentId());
            systemHelpCenterDTO.setTopId(topDo.getId());
            systemHelpCenterDTO.setTopName(topDo.getTitle());


            systemHelpCenterRepository.save(systemHelpCenterDTO);
        }

    }

    @Override
    public List<SystemHelpCenterDTO> listByKeyword(String keyword) {
        return systemHelpCenterRepository.findByContentPhrase(keyword);
    }
}
